/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yanzhenjie.andserver.provider;

import com.yanzhenjie.andserver.model.fileItemModel;
import com.yanzhenjie.andserver.util.FileUtils;
import com.yanzhenjie.hos.ResourceTable;
import com.yanzhenjie.swipelayout.SwipeLayout;

import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class ListProvider extends BaseItemProvider {
    private List<fileItemModel> arrayList;
    private Context getcontext;

    public ListProvider(List<fileItemModel> arrayList, Context getcontext) {
        this.arrayList = arrayList;
        this.getcontext = getcontext;
    }


    @Override
    public int getCount() {

        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return arrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component cponent = LayoutScatter.getInstance(getcontext).parse(ResourceTable.Layout_list_item, null, false);
        SwipeLayout swipeLayout = (SwipeLayout) cponent.findComponentById(ResourceTable.Id_swipeLayout);
        DirectionalLayout layout = (DirectionalLayout) cponent.findComponentById(ResourceTable.Id_bottom_wrapper);
        swipeLayout.initializeSwipe();
        swipeLayout.addDrag(layout);
        Text name = (Text) cponent.findComponentById(ResourceTable.Id_fliename);
        Text path = (Text) cponent.findComponentById(ResourceTable.Id_filepath);
        Text length = (Text) cponent.findComponentById(ResourceTable.Id_filelength);
        Image type_img = (Image) cponent.findComponentById(ResourceTable.Id_typeimg);
        Text tv_delete = (Text) cponent.findComponentById(ResourceTable.Id_text2);
        Text tv_share = (Text) cponent.findComponentById(ResourceTable.Id_Text1);
        name.setText(arrayList.get(i).getFilename());
        path.setText(arrayList.get(i).getFilepath());
        String filelength = FileUtils.FormetFileSize(arrayList.get(i).getFilelength());
        length.setText(filelength);
        if (!arrayList.get(i).getFiletype().isEmpty()) {
            if (arrayList.get(i).getFiletype().equals(".txt")) {
                type_img.setPixelMap(ResourceTable.Media_text);
            } else if (arrayList.get(i).getFiletype().equals(".docx")) {
                type_img.setPixelMap(ResourceTable.Media_docx);
            } else if (arrayList.get(i).getFiletype().equals(".png")) {

            } else if (arrayList.get(i).getFiletype().equals(".apk")) {
                type_img.setPixelMap(ResourceTable.Media_apk);
            } else if (arrayList.get(i).getFiletype().equals(".audio")) {
                type_img.setPixelMap(ResourceTable.Media_audio);
            } else if (arrayList.get(i).getFiletype().equals(".avi")) {
                type_img.setPixelMap(ResourceTable.Media_avi);
            } else if (arrayList.get(i).getFiletype().equals(".back")) {
                type_img.setPixelMap(ResourceTable.Media_back);
            } else if (arrayList.get(i).getFiletype().equals(".c")) {
                type_img.setPixelMap(ResourceTable.Media_c);
            } else if (arrayList.get(i).getFiletype().equals(".copy")) {
                type_img.setPixelMap(ResourceTable.Media_copy);
            } else if (arrayList.get(i).getFiletype().equals(".cpp")) {
                type_img.setPixelMap(ResourceTable.Media_cpp);
            } else if (arrayList.get(i).getFiletype().equals(".dir")) {
                type_img.setPixelMap(ResourceTable.Media_dir);
            } else if (arrayList.get(i).getFiletype().equals(".doc")) {
                type_img.setPixelMap(ResourceTable.Media_doc);
            } else if (arrayList.get(i).getFiletype().equals(".excel")) {
                type_img.setPixelMap(ResourceTable.Media_excel);
            } else if (arrayList.get(i).getFiletype().equals(".flv")) {
                type_img.setPixelMap(ResourceTable.Media_flv);
            } else if (arrayList.get(i).getFiletype().equals(".gif")) {
                type_img.setPixelMap(ResourceTable.Media_gif);
            } else if (arrayList.get(i).getFiletype().equals(".gz")) {
                type_img.setPixelMap(ResourceTable.Media_gz);
            } else if (arrayList.get(i).getFiletype().equals(".html")) {
                type_img.setPixelMap(ResourceTable.Media_html);
            } else if (arrayList.get(i).getFiletype().equals(".rar")) {
                type_img.setPixelMap(ResourceTable.Media_rar);
            } else if (arrayList.get(i).getFiletype().equals(".zip")) {
                type_img.setPixelMap(ResourceTable.Media_zip);
            }
        } else {
            type_img.setPixelMap(ResourceTable.Media_other_file);
        }

        tv_delete.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                arrayList.remove(i);
                notifyDataSetItemChanged(i);
            }
        });
        tv_share.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

            }
        });

        return cponent;
    }
}
